package com.kantboot.functional.verification.code.service.impl;

import com.kantboot.functional.template.repository.FunctionalTemplateRepository;
import com.kantboot.functional.verification.code.domain.entity.FunctionalVerificationCodeTemplate;
import com.kantboot.functional.verification.code.repository.FunctionalVerificationCodeTemplateRepository;
import com.kantboot.functional.verification.code.service.IFunctionalVerificationCodeTemplateService;
import com.kantboot.global.exception.GlobalException;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class FunctionalVerificationCodeTemplateService implements IFunctionalVerificationCodeTemplateService {

    @Resource
    private FunctionalVerificationCodeTemplateRepository repository;

    @Resource
    private FunctionalTemplateRepository templateRepository;

    private void insertCheck(FunctionalVerificationCodeTemplate entity) {
        // 判断类型编码是否为空
        if (entity.getTypeCode() == null) {
            throw GlobalException.functionalVerificationCode.TYPE_CODE_IS_NOT_NULL;
        }
        // 判断场景编码是否为空
        if (entity.getSceneCode() == null) {
            throw GlobalException.functionalVerificationCode.SCENE_CODE_IS_NOT_NULL;
        }
        // 判断模板编码是否为空
        if (entity.getTemplateCode() == null) {
            throw GlobalException.functionalVerificationCode.TEMPLATE_CODE_IS_NOT_NULL;
        }
        // 判断模板是否存在
        if (templateRepository.findByCode(entity.getTemplateCode()) == null) {
            throw GlobalException.functionalVerificationCode.TEMPLATE_NOT_FOUND;
        }
        // 判断类型编码和场景编码的搭配是否重复
        long count = repository.countByTypeCodeAndSceneCode(entity.getTypeCode(), entity.getSceneCode());
        if (count > 0) {
            throw GlobalException.functionalVerificationCode.TYPE_CODE_AND_SCENE_CODE_IS_REPEAT;
        }
    }

    private void updateCheck(FunctionalVerificationCodeTemplate entity, FunctionalVerificationCodeTemplate old) {
        // 获取编码
        String typeCode = entity.getTypeCode();
        String sceneCode = entity.getSceneCode();
        // 判断类型编码是否为空
        if (typeCode == null) {
            typeCode = old.getTypeCode();
        }
        // 判断场景编码是否为空
        if (sceneCode == null) {
            sceneCode = old.getSceneCode();
        }

        // 判断类型编码和场景编码的搭配是否重复
        long count = repository.countByTypeCodeAndSceneCode(typeCode, sceneCode);
        if (count > 0) {
            throw GlobalException.functionalVerificationCode.TYPE_CODE_AND_SCENE_CODE_IS_REPEAT;
        }
    }

    @Override
    public FunctionalVerificationCodeTemplate save(FunctionalVerificationCodeTemplate entity) {
        if (entity.getId() == null) {
            // 检查
            insertCheck(entity);
            return repository.save(entity);
        }
        // 查询是否修改了类型编码或场景编码
        FunctionalVerificationCodeTemplate old = repository.findById(entity.getId()).orElse(null);
        if (old == null) {
            // 检查
            insertCheck(entity);
            return repository.save(entity);
        }
        // 如果模板编码修改了
        if (entity.getTemplateCode() != null && !old.getTemplateCode().equals(entity.getTemplateCode())) {
            // 检查
            if (templateRepository.findByCode(entity.getTemplateCode()) == null) {
                throw GlobalException.functionalVerificationCode.TEMPLATE_NOT_FOUND;
            }
        }

        // 判断是否修改了类型编码或场景编码
        if ((entity.getTypeCode() != null && !old.getTypeCode().equals(entity.getTypeCode())) ||
            (entity.getSceneCode() != null && !old.getSceneCode().equals(entity.getSceneCode()))) {
            // 检查
            updateCheck(entity, old);
        }

        return repository.save(entity);
    }

}
